﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Util;

namespace DAL
{
    public class IMG
    {
        public int id { get; set; }
        public String title { get; set; }
        public String imgurl { get; set; }
        public String description { get; set; }
        public String pubdate { get; set; }
        public String category { get; set; }
        public String imgName = String.Empty;
        public String imgName1 = String.Empty;
        public override bool Equals(object obj)
        {
            IMG img = obj as IMG;
            if (this.title == img.title && this.imgurl == img.imgurl && this.pubdate == img.pubdate)
            {
                return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            int hashcode_title = String.IsNullOrEmpty(title) ? 0 : title.GetHashCode();
            int hashcode_imgurl = String.IsNullOrEmpty(imgurl) ? 0 : imgurl.GetHashCode();
            int hashcode_pubdate = String.IsNullOrEmpty(pubdate) ? 0 : pubdate.GetHashCode();
            return hashcode_title ^ hashcode_imgurl ^ hashcode_pubdate;
        }

        public string ToCsv(String relative)
        {
            if (!String.IsNullOrEmpty(imgurl))
            {
                DateTime dt = DateTime.MinValue;
                DateTime.TryParse(pubdate.Trim(), out dt);
                string stamp = dt.ToString("yyMMddHHmmss");
                String name = imgurl.Split('/').ToList().Last();
                imgName1 = category + "_" + stamp + "_" + name;
                imgName = relative + "\\" + category + "_" + stamp + "_" + name;
            }

            StringBuilder buff = new StringBuilder();
            buff.Append(CsvUtil.Format(category));
            buff.Append(CsvUtil.Format(title));
            buff.Append(CsvUtil.Format(description));
            buff.Append(CsvUtil.Format(pubdate));
            buff.Append(CsvUtil.Format(imgurl));
            buff.Append(CsvUtil.Format(imgName));
            buff.Append(CsvUtil.Format(imgName1));
            String temp = buff.ToString();
            String line = temp.Remove(temp.Length - 1, 1);
            return line;
        }
    }
}
